package test.core.safe;

import java.io.IOException;
import java.security.Key;

import javax.crypto.spec.IvParameterSpec;

import hyl.core.MyFun;
import hyl.core.safe.MyAes;
import hyl.core.safe.MyBase64;
import hyl.core.safe.RsaCert;
import hyl.core.safe.MyDes;
import hyl.core.safe.MyRsa;
import hyl.core.safe.MySecret;
import hyl.core.safe.Rsa;
import jnr.ffi.Struct.int16_t;

public class testSecret {
	public static void main(String[] args) throws IOException {
		long start = MyFun.getMs();
		 testMyCert1();

		//testMyAes2();
		// testMyDes();

		// System.out.println( MyBase64.encode(MySecret.hexStr2Bytes( "ae12344")));
		// System.out.println( MyBase64.encode(MyFun.decodeHex( "ae12344")));
		long len = MyFun.getMs() - start;
		System.out.println(len);
	}

	public static void testMyAes2() {
		String str = "芸swee看ad付款是的发~!#$$#&#&^^%**() {}:>?<|送的合并t";
		// String str = "passwordtoecrypt";
		// 打印原文
		// System.out.println("原文：" + str);

		// System.out.println("密钥：" +ky);
		// keycodes;
		try {
			// 生成随机密钥 CYpdx13z28dMblqXJBKWFeKD
			// byte[] keycodes = MySecret.getN位随机数(16).getBytes(); //
			// MySecret.createKey("AES",128).getEncoded();
			// byte[] iv1 = MySecret.getN位随机数(16).getBytes();
			// 打印密钥
			String key1 = "com.sevenlin.foo.foo.foo";
			String iv2 = "0123456789abcdef";
			Key key = MyAes.generateKey(key1);
			IvParameterSpec iv = MyAes.generateIv(iv2);
			for (int i = 0; i < 1000; i++) {
				byte[] enb = MyAes.f加密(str.getBytes(), key, iv);
				// System.out.println("密文：" + enb);
				// 解密密文
				byte[] data2 = MyAes.f解密(enb, key, iv);
				// 打印原文
				System.out.println("解密后：" + new String(data2));
			}
			// long lend = MyFun.getMs();
			// System.out.println(lend - lstart);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void testMyDes() {
		String 明文 = "芸swee看ad付款是的发~!#$$#&#&^^%**() {}:>?<|送的合并t";
		// String str = "passwordtoecrypt";
		// 打印原文
		// System.out.println("原文：" + str);

		// 密钥
		// keycodes;
		try {
			// 生成随机密钥

			//Key key2 = MySecret.createKey("DES", "123456712289012345678290");// 指定的
			// Key keycodes =MySecret.createKey("DES");//变化的
			Key key2 = MyDes.generateKey("123456712289012345678290");
			for (int i = 0; i < 1000; i++) {
				// 打印密钥 watG93aMzWg=
				// System.out.println("密钥：" + MyBase64.encode(keycodes.getEncoded()));
				// String 密文 = MyDes.f加密(明文.getBytes(),"com.sevenlin.foo.key");
				String 密文 = MyDes.f加密64(明文, key2);
				// System.out.println("密文：" + 密文);
				// 解密密文
				// byte[] data2 = MyDes.f解密(密文,"com.sevenlin.foo.key");
				String data2 = MyDes.f加密64(密文, key2);
				// 打印原文
				System.out.println("解密后：" + data2);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void testMyCert1() throws IOException {
		 RsaCert k1 = RsaCert.getInstance();
		k1.load公私钥PEM("./cert");
		String s = "经常V了时间为哦累托if给收到经理看看估!@#@#%^%*&%*^()计是你的g";

		for (int i = 0; i < 1000; i++) {
			byte[] jm = MyRsa.f公钥加密(k1.get公钥(),s.getBytes());
			byte[] mm = MyRsa.f私钥解密(k1.get私钥(),jm);
			MyFun.print(new String(mm), i);
		}
	}

	public static void testMyCert() throws IOException {
		// 创建公钥和私钥
		/*
		*/
		Rsa k;
		// k=MyCert.getInstance().iniKey();//随机生成
		k = new Rsa("test","");
;// 随机生成,长度可以设置
		// k=MyCert.getInstance().iniKey("固定");//生成固定的key,没有太大意义

//		byte[] publicKey = k.getPubKey().getEncoded();
//
//		byte[] privateKey = k.getPriKey().getEncoded();

		String publicKeyString = k.get公钥Base64();
		// 得到私钥字符串
		String privateKeyString = k.get私钥Base64();
		System.out.println(publicKeyString);
		System.out.println(privateKeyString);

		k.save公私钥PEM("./cert");
		// 读取指定文件夹下面的密钥

	}

}

/*
 * //npm install crypto-js var CryptoJS = require("crypto-js");
 * 
 * var secretKey = 'com.sevenlin.foo.key';
 * 
 * var afterEncrypt = CryptoJS.DES.encrypt('passwordtoecrypt',
 * CryptoJS.enc.Utf8.parse(secretKey), { mode: CryptoJS.mode.ECB, padding:
 * CryptoJS.pad.NoPadding }).toString()
 * 
 * console.log(afterEncrypt);//7D7RsPHmNSlBAaEVgEyE4Q==
 * 
 * var afterDecrypt = CryptoJS.DES.decrypt(afterEncrypt,
 * CryptoJS.enc.Utf8.parse(secretKey), { mode: CryptoJS.mode.ECB, padding:
 * CryptoJS.pad.NoPadding }).toString(CryptoJS.enc.Utf8);
 * 
 * console.log(afterDecrypt);//passwordtoecrypt
 * 
 * secretKey = 'com.sevenlin.foo.foo.foo'; var iv =
 * CryptoJS.enc.Utf8.parse('0123456789abcdef'); afterEncrypt =
 * CryptoJS.AES.encrypt('passwordtoecrypt', CryptoJS.enc.Utf8.parse(secretKey),
 * { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding
 * }).toString()
 * 
 * console.log(afterEncrypt);//lW0IEJpIBiG+ZnfkQGmf4w== afterDecrypt =
 * CryptoJS.AES.decrypt(afterEncrypt, CryptoJS.enc.Utf8.parse(secretKey), { iv:
 * iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding
 * }).toString(CryptoJS.enc.Utf8); console.log(afterDecrypt);//passwordtoecrypt
 */
